
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {number[]} nums
 * @return {TreeNode}
 */

 function makeBST(nums,start,end){
  if(start>end){
      return null
  }
  let minIndex = Math.floor((start+end)/2);
  let root = new TreeNode(nums[minIndex]);

  root.left = makeBST(nums,start,minIndex-1);
  root.right = makeBST(nums,minIndex+1,end);
  return root
}

var sortedArrayToBST = function(nums) {
  return makeBST(nums,0,nums.length-1);
};